package com.yunbao.hujin.mymvptest.login.presenter;

import android.os.Handler;
import android.os.Looper;

import com.yunbao.hujin.mymvptest.login.model.IUser;
import com.yunbao.hujin.mymvptest.login.model.UserModel;
import com.yunbao.hujin.mymvptest.login.view.ILoginView;

/**
 * presenter层 de 逻辑实现
 */

public class LoginPresenterCompl implements ILoginPresenter{

    private ILoginView iLoginView;
    private IUser user;
    private Handler handler;        //模拟登录耗时操作

    public LoginPresenterCompl(ILoginView iLoginView) {
        this.iLoginView = iLoginView;
        initUser();
        handler = new Handler(Looper.getMainLooper());
    }

    private void initUser() {
        user = new UserModel("mvp", "mvp");
    }


    /******** __________ILoginPresenter接口方法________*************/
    @Override
    public void clear() {
        //本应该处理clear逻辑，但是却又抛给view层次来处理，则即在activity中进行逻辑处理
        iLoginView.onClearText();
    }

    @Override
    public void doLogin(String name, String password) {
        //登录逻辑在presenterCompl中实现--处理登录返回结果利用iLoginView.onLoginResult(result, code);方法
        //并在Activity中处理
        Boolean isLoginSuccess = true;
        int code = user.checkUserValidity(name, password);  //判断逻辑在UserModel实现的view方法中执行
        if (code != 0) {
            isLoginSuccess = false;
        }
        Boolean result = isLoginSuccess;

        handler.postDelayed((Runnable) () -> {
            iLoginView.onLoginResult(result, code);     //处理登录返回结果
        }, 2000);
    }

    @Override
    public void setProgressBarVisiblity(int visiblity) {
        //本应该处理clear逻辑，但是却又抛给view层次来处理，则即在activity中进行逻辑处理
        iLoginView.onSetProgressBarVisibility(visiblity);
    }
}
